package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.ActivityInListData;
import com.wbao.dianniu.utils.PictureDownloadUtils;

/**
 * 已加入活动列表
 *
 * @author yejianjun
 */
public class ActivityInListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements View.OnClickListener {
    private Context mContext;
    private List<ActivityInListData> list = new ArrayList<ActivityInListData>();
    private LayoutInflater mInflater;
    //    private ActInConfirmManger manager;
    //是否有热门置顶显示
    private int topCounts = 0;
    private static final int TYPE_ITEM = 0;
    private static final int TYPE_FOOTER = 1;

    //上拉加载更多
    public static final int PULLUP_LOAD_MORE = 0;
    //正在加载中
    public static final int LOADING_MORE = 1;
    //没有加载更多 隐藏
    public static final int NO_LOAD_MORE = 2;
    //上拉加载更多状态-默认为0
    private int mLoadMoreStatus = 0;
    private OnItemClickListener onItemClickListener;


    public ActivityInListAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(mContext);
    }

    public List<ActivityInListData> getList() {
        return list;
    }

    /*@Override
    public int getCount() {
        if (null == list)
            return 0;
        else
            return list.size();
    }*/

    public void clear() {
        if (null != list) {
            list.clear();
        }
    }


    public void updateVisibileView(int index, ListView listview) {
        //得到第一个可见item项的位置
//        int visiblePosition = listview.getFirstVisiblePosition();
//        //得到指定位置的视图，对listview的缓存机制不清楚的可以去了解下
//        View view = listview.getChildAt(index - visiblePosition);
//        Holder holder = (Holder) view.getTag();
//        int lastPosition = listview.getLastVisiblePosition();
//        for (int i = visiblePosition; i <= lastPosition; i++) {
//            getView(i,view)
//        }

    }


    public void statusChange(int activityId, int accountId, int status) {
        if (null == list)
            return;
        for (int i = 0; i < list.size(); i++) {
            if (activityId == list.get(i).getActivityId() && accountId == list.get(i).getAccountId()) {
                list.get(i).setStatus(status);
                break;
            }
        }
        notifyDataSetChanged();

    }


    public void addData(List<ActivityInListData> addList) {
        for (int i = 0; i < addList.size(); i++) {
            list.add(addList.get(i));
        }
    }

    /*@Override
    public Object getItem(int position) {
        return list.get(position);
    }*/

    @Override
    public int getItemCount() {
        return list.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position + 1 == getItemCount()) {
            //最后一个item设置为footerView
            return TYPE_FOOTER;
        } else {
            return TYPE_ITEM;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ITEM) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.already_join_item, parent, false);
            return new ItemViewHolder(view);
        } else if (viewType == TYPE_FOOTER) {
            View itemView = mInflater.inflate(R.layout.load_more_footview_layout, parent, false);
            return new FooterViewHolder(itemView);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ItemViewHolder) {
            ItemViewHolder itemViewHolder = (ItemViewHolder) holder;
            ActivityInListData data = list.get(position);
            PictureDownloadUtils.displayHeadImage(mContext, itemViewHolder.head, data.getHeadPic());
            itemViewHolder.name.setText(data.getRealName());
            itemViewHolder.label.setText(data.getLabel() == null ? "" : data.getLabel());
            if (1 == data.getStatus()) {
                itemViewHolder.alreadyBtn.setVisibility(View.GONE);
                itemViewHolder.agreedBtn.setVisibility(View.VISIBLE);
                itemViewHolder.refuseBtn.setVisibility(View.VISIBLE);
            } else if (2 == data.getStatus()) {
                itemViewHolder.alreadyBtn.setVisibility(View.VISIBLE);
                itemViewHolder.agreedBtn.setVisibility(View.GONE);
                itemViewHolder.refuseBtn.setVisibility(View.GONE);
                itemViewHolder.alreadyBtn.setText(mContext.getResources().getString(R.string.already_agreed));
            } else if (3 == data.getStatus()) {
                itemViewHolder.alreadyBtn.setVisibility(View.VISIBLE);
                itemViewHolder.agreedBtn.setVisibility(View.GONE);
                itemViewHolder.refuseBtn.setVisibility(View.GONE);
                itemViewHolder.alreadyBtn.setText(mContext.getResources().getString(R.string.already_refused));
            }
            itemViewHolder.agreedBtn.setOnClickListener(this);
            itemViewHolder.refuseBtn.setOnClickListener(this);
            itemViewHolder.callTV.setOnClickListener(this);
            itemViewHolder.msnTV.setOnClickListener(this);
            itemViewHolder.msgTV.setOnClickListener(this);


            itemViewHolder.agreedBtn.setTag(R.id.already_agreed, position);
            itemViewHolder.refuseBtn.setTag(R.id.already_refuse, position);
            itemViewHolder.callTV.setTag(R.id.already_call, position);
            itemViewHolder.msnTV.setTag(R.id.already_send_msn, position);
            itemViewHolder.msgTV.setTag(R.id.already_send_msg, position);

        } else if (holder instanceof FooterViewHolder) {
            FooterViewHolder footerViewHolder = (FooterViewHolder) holder;
            switch (mLoadMoreStatus) {
                case PULLUP_LOAD_MORE:
                    footerViewHolder.mPbLoad.setVisibility(View.GONE);
                    footerViewHolder.mTvLoadText.setText("上拉加载更多...");
                    footerViewHolder.mLoadLayout.setVisibility(View.VISIBLE);
                    break;
                case LOADING_MORE:
                    footerViewHolder.mPbLoad.setVisibility(View.VISIBLE);
                    footerViewHolder.mTvLoadText.setText("正在加载...");
                    footerViewHolder.mLoadLayout.setVisibility(View.VISIBLE);
                    break;
                case NO_LOAD_MORE:
                    //隐藏加载更多
                    if (list.size() >= 10) {
                        footerViewHolder.mTvLoadText.setText("没有更多数据");
                        footerViewHolder.mPbLoad.setVisibility(View.GONE);
                        footerViewHolder.mLoadLayout.setVisibility(View.VISIBLE);
                    } else {
                        footerViewHolder.mLoadLayout.setVisibility(View.GONE);
                    }
                    break;
                default:
                    break;
            }

        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

   /* @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.already_join_item, null);
            holder = new Holder();

            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }
        final ActivityInListData data = list.get(position);
        PictureDownloadUtils.displayHeadImage(mContext, holder.head, data.getHeadPic());
        holder.name.setText(data.getRealName());
        holder.label.setText(data.getLabel() == null ? "" : data.getLabel());
        if (1 == data.getStatus()) {
            holder.alreadyBtn.setVisibility(View.GONE);
            holder.agreedBtn.setVisibility(View.VISIBLE);
            holder.refuseBtn.setVisibility(View.VISIBLE);
        } else if (2 == data.getStatus()) {
            holder.alreadyBtn.setVisibility(View.VISIBLE);
            holder.agreedBtn.setVisibility(View.GONE);
            holder.refuseBtn.setVisibility(View.GONE);
            holder.alreadyBtn.setText(mContext.getResources().getString(R.string.already_agreed));
        } else if (3 == data.getStatus()) {
            holder.alreadyBtn.setVisibility(View.VISIBLE);
            holder.agreedBtn.setVisibility(View.GONE);
            holder.refuseBtn.setVisibility(View.GONE);
            holder.alreadyBtn.setText(mContext.getResources().getString(R.string.already_refused));
        }
//        holder.agreedBtn.setOnClickListener(new MyClickListener(data));
//        holder.refuseBtn.setOnClickListener(new MyClickListener(data));
        *//*holder.callTV.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                CustomDialog.Builder builder = new CustomDialog.Builder(mContext);
                builder.setTitle("确认拨打电话：" + data.getMobile());
                builder.setPositiveButton(mContext.getResources().getString(R.string.confirm),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                                //调用拨打电话
                                PermissionUtils.getInstance().intentToCall(mContext, data.getMobile());
                            }
                        });
                builder.setNegativeButton(mContext.getResources().getString(R.string.cancel),
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.dismiss();
                            }
                        });
                builder.create().show();
            }
        });*//*
        *//*holder.msnTV.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // 发短信
                PermissionUtils.getInstance().intentToSendSMS(mContext, data.getMobile(), "");
            }
        });*//*
        *//*holder.msgTV.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                //跳转到聊天界面
                String appkey = mContext.getResources().getString(R.string.app_key);
                GlobalUserData userData = new GlobalUserData();
                LoginSampleHelper.getInstance().openChat(mContext, userData.getEnvironemt() + data.getAccountId() + "", appkey);
            }
        });*//*
        return convertView;
    }*/

    /**
     * 更新加载更多状态
     *
     * @param status
     */
    public void changeMoreStatus(int status) {
        mLoadMoreStatus = status;
        notifyDataSetChanged();
    }

    /**
     * 设置回调监听
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.onItemClickListener = listener;
    }


    @Override
    public void onClick(View v) {
        onItemClickListener.itemClick(v);
    }

    class Holder {
        public ImageView head;
        public TextView name;
        public TextView label;
        public TextView agreedBtn;
        public TextView refuseBtn;
        public TextView alreadyBtn;
        public TextView callTV;
        public TextView msnTV;
        public TextView msgTV;
    }


    /*class MyClickListener implements OnClickListener {

        private ActivityInListData data;

        public MyClickListener(ActivityInListData data) {
            this.data = data;
        }

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.already_agreed:    // 同意
                    manager.inconfirm(data.getAccountId(), data.getActivityId(), 2);
                    ActivityHelperManager.getInstance().IsChanged(data.getActivityId(), data.getAccountId(), 4);
                    statusChange(data.getActivityId(), data.getAccountId(), 2);
                    break;
                case R.id.already_refuse:    //拒绝
                    manager.inconfirm(data.getAccountId(), data.getActivityId(), 3);
                    ActivityHelperManager.getInstance().IsChanged(data.getActivityId(), data.getAccountId(), 4);
                    statusChange(data.getActivityId(), data.getAccountId(), 3);
                    break;
                default:
                    break;
            }
        }

    }*/

    public interface OnItemClickListener {
        void itemClick(View view);
    }

    class ItemViewHolder extends RecyclerView.ViewHolder {
        public ImageView head;
        public TextView name;
        public TextView label;
        public TextView agreedBtn;
        public TextView refuseBtn;
        public TextView alreadyBtn;
        public TextView callTV;
        public TextView msnTV;
        public TextView msgTV;

        public ItemViewHolder(View itemView) {
            super(itemView);
            head = itemView.findViewById(R.id.already_join_imageview);
            name = itemView.findViewById(R.id.already_join_name);
            label = itemView.findViewById(R.id.already_join_label);
            agreedBtn = itemView.findViewById(R.id.already_agreed);
            refuseBtn = itemView.findViewById(R.id.already_refuse);
            alreadyBtn = itemView.findViewById(R.id.already_process_btn);
            callTV = itemView.findViewById(R.id.already_call);
            msnTV = itemView.findViewById(R.id.already_send_msn);
            msgTV = itemView.findViewById(R.id.already_send_msg);
        }
    }

    class FooterViewHolder extends RecyclerView.ViewHolder {
        ProgressBar mPbLoad;
        TextView mTvLoadText;
        LinearLayout mLoadLayout;

        public FooterViewHolder(View itemView) {
            super(itemView);
            mPbLoad = itemView.findViewById(R.id.pbLoad);
            mTvLoadText = itemView.findViewById(R.id.tvLoadText);
            mLoadLayout = itemView.findViewById(R.id.loadLayout);
        }
    }

}
